import 'package:flutter/material.dart';

import '../../../helper/global/user/logic.dart';
import '../../../helper/utils/util.dart';
import 'index.dart';
import 'widgets/code_field.dart';
import 'widgets/widgets.dart';

class LoginPage extends GetView<LoginController> {
  const LoginPage({Key? key}) : super(key: key);

  // 主视图
  Widget _buildView() {
    var state = controller.state;
    return Container(
      decoration: BoxDecoration(
        image: DecorationImage(
          image: loadImageProvider(url: 'log_bg.png'.imgPathCommon),
          fit: BoxFit.cover,
        ),
      ),
      child: ListView(
        children: [
          // Row(
          //   mainAxisAlignment: MainAxisAlignment.end,
          //   children: [
          //     IconButton(
          //       onPressed: () {
          //         Get.offAll(() => HomePage());
          //       },
          //       icon: Icon(
          //         Icons.close,
          //         color: const Color(0xffdcdcdc),
          //         size: 30.r,
          //       ),
          //     ),
          //   ],
          // ),
          SizedBox(
            height: 30.r,
          ),
          Center(
            child: loadAvater(
              url: UserController.instance.md.icon,
              fit: BoxFit.contain,
              size: 150.r,
            ),
          ),
          Container(
            alignment: Alignment.center,
            child: Text(
              UserController.instance.md.text ?? '',
              style: TextStyle(
                color: Colors.white,
                fontSize: 18.sp,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
          SizedBox(
            height: 20.r,
          ),
          LoginField(
            controller: state.accountCtrl,
            title: '账号',
            placeholder: '请输入账号',
            padding: EdgeInsets.symmetric(horizontal: 30.r),
            errText: state.accountError,
            titleColor: Colors.white,
            onChange: (p0) {
              state.accountError = null;
              controller.update();
            },
          ),
          SizedBox(
            height: 10.r,
          ),
          state.isPwdLogin
              ? LoginField(
                  controller: state.pwdCtrl,
                  title: '密码',
                  placeholder: '请输入密码',
                  padding: EdgeInsets.symmetric(horizontal: 30.r),
                  isPwd: true,
                  errText: state.pwdError,
                  titleColor: Colors.white,
                  onChange: (p0) {
                    state.pwdError = null;
                    controller.update();
                  },
                )
              : Container(
                  padding: EdgeInsets.symmetric(horizontal: 30.r),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '验证码',
                        style: TextStyle(
                            color: const Color(0xff333333), fontSize: 15.sp),
                      ),
                      SizedBox(
                        height: 8.r,
                      ),
                      ICodeField(
                        smsClickListener: () async {
                          return false;
                        },
                        isLineStyle: false,
                        errText: state.pwdError,
                        onChange: (p0) {
                          state.pwdError = null;
                          controller.update();
                        },
                      ),
                    ],
                  ),
                ),
          Padding(
            padding: EdgeInsets.symmetric(
              horizontal: 30.r,
            ),
            child: ReadDealWidget(
              actionAnimate: (p0) {
                state.readAnim = p0;
              },
            ),
          ),
          SizedBox(
            height: 30.r,
          ),
          InkWell(
            onTap: () {
              if (!state.isReaded) {
                var p0 = state.readAnim!;
                if (p0.isAnimating) {
                  return;
                }
                p0.repeat();
                Future.delayed(const Duration(milliseconds: 600)).then((value) {
                  p0.stop();
                });
              } else {
                controller.actionLogin();
              }
            },
            child: Container(
              margin: EdgeInsets.symmetric(horizontal: 30.r),
              height: 50.r,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Colors.blue,
                borderRadius: BorderRadius.circular(5.r),
              ),
              child: Text(
                '登录',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 17.sp,
                ),
              ),
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              InkWell(
                onTap: () {
                  controller.toForgetPwd();
                },
                child: Container(
                  height: 40.r,
                  alignment: Alignment.centerRight,
                  margin: EdgeInsets.only(right: 30.r),
                  child: Text(
                    '忘记密码',
                    style: TextStyle(
                      color: const Color(0xffdcdcdc),
                      fontSize: 14.sp,
                    ),
                  ),
                ),
              ),
            ],
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LoginController>(
      builder: (_) {
        return WillPopScope(
            child: Scaffold(
              backgroundColor: Colors.white,
              body: _buildView(),
            ),
            onWillPop: () async {
              return false;
            });
      },
    );
  }
}
